package org.lojzes.minstruts.servlet;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lojzes.minstruts.action.Action;
import org.lojzes.minstruts.reflect.ClassFull;
import org.lojzes.minstruts.xml.XMLBean;

public class ActionServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 doPost(req, resp);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Map<String,XMLBean> xmlBean = (Map<String,XMLBean>)req.getServletContext().getAttribute("xmlBeanMap");
		String servletPath =req.getServletPath();
		String actionType =xmlBean.get(servletPath).getActionType();
		String beanClass =xmlBean.get(servletPath).getBeanClass();
		Action action = ClassFull.getInstance().fullAction(actionType);
		String forward = action.execute(req, resp, ClassFull.getInstance().fullForm(beanClass, req));
		req.getRequestDispatcher(xmlBean.get(servletPath).getForwards().get(forward)).forward(req, resp);
	}

}
